<template>
  <div>
    <div class="app-container">
      <el-col :span="24">
        <el-form size="mini" ref="postForm" :model="postForm" :label-position="'right'" label-width="80px">
          <el-row type="flex" justify="space-between" :gutter="20">

            <el-col :span="10">
              <el-form-item label="名称:">
                <el-input v-model="postForm.name" placeholder="请输入内容"/>
              </el-form-item>
            </el-col>
            <el-col :span="14">
              <el-form-item>
                <el-button type="primary" size="mini" @click="query()">查询</el-button>
                <el-button type="warning" size="mini" @click="addInfo()">新增</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-table :height="tableHeight" :data="tableData" border>
          <el-table-column type="index" align="center" width="50"></el-table-column>
          <el-table-column
            prop="name"
            label="名称"
          />
          <el-table-column
            fixed="right"
            label="操作" width="300" align="center"
          >
            <template slot-scope="scope">
              <el-button type="success" size="mini" @click="editInfo(scope.$index,scope.row)">编辑</el-button>
              <el-button type="danger" size="mini" @click="delInfo(scope.$index,scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" class="row-bg" justify="center">
          <el-pagination
            background
            layout="total,prev,pager,next,jumper,sizes"
            :total="total"
            @size-change="handleSizeChange"
            :current-page.sync="currentPage"
            :page-sizes="[15, 30, 45, 60, 75,90,105,120,135,150]"
            :page-size="pageSize"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-col>
    </div>

    <el-dialog
      :title="title"
      :visible.sync="centerDialogVisible"
      center
    >
      <el-form ref="info" :rules="rules" :model="info" label-position="right" label-width="100px">
        <el-form-item label="名称:" prop="name">
          <el-input v-model="info.name" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrEdit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
.el-table {
  background: oldlace;
}

.el-table {
  background: #f0f9eb;
}
</style>

<script>
import {page, getInfo, delInfo, addOrEdit, getList} from '@/api/system/protectiveWear.js'

export default {
  data() {
    return {
      tableHeight: window.innerHeight - 180,
      // 总条数
      total: 0,
      // 每页显示个数
      pageSize: 15,
      // 页码
      currentPage: 1,
      readExcelDialog: false,
      postForm: {},
      info: {},
      tableData: [],
      readExcelFile: null,
      name: null,
      id: null,
      options: [],
      editIndex: 0,
      centerDialogVisible: false,
      title: '新增',
      rules: {
        name: [{required: true, message: '请输入名称', trigger: 'blur'}],
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
    },
    // 分页查询
    handleCurrentChange(val) {
      this.currentPage = val
      this.getData()
    },
    // 新增
    addInfo() {
      this.title = '新增'
      this.info = {
        type: 2
      }
      this.centerDialogVisible = true
    },
    /**
     * 编辑
     * @param row
     */
    editInfo(index, row) {
      this.editIndex = index
      this.title = '编辑'
      this.centerDialogVisible = true
      getInfo(row.id).then(response => {
        this.info = response.data
      })
    },
    delInfo(index, row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delInfo(row.id).then(response => {
          this.tableData.splice(index, 1)
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    },
    // 查询条件
    query() {
      this.name = this.postForm.name
      this.currentPage = 1
      this.getData()
    },
    getData() {
      // 请求分页参数
      page(this.pageSize, this.currentPage, this.name, 2).then(response => {
        this.total = parseInt(response.count)
        this.tableData = response.data
      })
    },
    addOrEdit: function () {
      this.$refs['info'].validate((valid) => {
        if (valid) {
          addOrEdit(this.info.id, this.info.name, this.info.type).then(response => {
            this.centerDialogVisible = false
            if (this.info.id == null) {
              // 新增
              this.getData()
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            } else {
              // 编辑
              this.tableData.splice(this.editIndex, 1, response.data)
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
            }
          })
        }
      })
    }
  }
}
</script>

